﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace sXNAke
{
    class Pacman : AnimatedGameEntity
    {
        Direction actualDirection = Direction.NONE;

        int points;

        int lifes;

        public Vector2 hudPosition;

        public Color hudColor;

        /// <summary>
        /// The current frame
        /// </summary>
        public Vector2 currentFrame;

        /// <summary>
        /// define se o player pegou o power up.
        /// </summary>
        private bool isPoweredUp;

        /// <summary>
        /// The frame time
        /// </summary>
        private float frameTime = 250;

        /// <summary>
        /// The last frame time
        /// </summary>
        private double lastFrameTime = 0;

        /// <summary>
        /// Flip the sprite sprite on X coordinate
        /// </summary>
        private SpriteEffects spriteEffects;

        /// <summary>
        ///  ANIMATIONS
        /// </summary>
        private Animation chewingLeft;
        private Animation chewingRight;
        private Animation chewingUp;
        private Animation chewingDown;
        private Animation dying;

        public Pacman(Texture2D image,GameWindow Window)
            : base(image, Vector2.Zero)
        {
            setAnimations();

            this.Window = Window;
            this.Velocity = new Vector2(4, 4);
            this.position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2+30);
            this.points = 0;
            this.lifes = Game1.QTD_INICIAL_VIDAS;

            this.currentFrame = new Vector2(0,0);
            this.spriteEffects = SpriteEffects.None;
        }
        /// <summary>
        /// Config all animations
        /// </summary>
        private void setAnimations()
        {
            this.chewingLeft = new Animation("chewingLeft",new Vector2(39,38), 250, 0, 0, 2, Axis.X);
            this.chewingRight = new Animation("chewingRight", new Vector2(39, 38), 250, 0, 1, 2, Axis.X);
            this.chewingUp = new Animation("chewingUp", new Vector2(39, 38), 250, 0, 2, 2, Axis.X);
            this.chewingDown = new Animation("chewingDown", new Vector2(39, 38), 250, 0, 3, 2, Axis.X);
            this.dying = new Animation("dying", new Vector2(39, 38), 250, 0, 3, 5, Axis.X);

            this.addAnimation(chewingLeft);
            this.addAnimation(chewingRight);
            this.addAnimation(chewingDown);
            this.addAnimation(chewingUp);
            this.addAnimation(dying);

            setCurrentAnimation("chewingLeft");
        }
        public override void update(GameTime gameTime)
        {
            base.update(gameTime);

            if (Game1.keyboardState.IsKeyDown(Keys.Right) && !Game1.oldKeyboardState.IsKeyDown(Keys.Right))
            {
                setCurrentAnimation("chewingRight");
                actualDirection = Direction.RIGHT;
            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Left) && !Game1.oldKeyboardState.IsKeyDown(Keys.Left))
            {
                setCurrentAnimation("chewingLeft");
                actualDirection = Direction.LEFT;
            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Up) && !Game1.oldKeyboardState.IsKeyDown(Keys.Up))
            {
                setCurrentAnimation("chewingUp");
                actualDirection = Direction.UP;
            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Down) && !Game1.oldKeyboardState.IsKeyDown(Keys.Down))
            {
                setCurrentAnimation("chewingDown");
                actualDirection = Direction.DOWN;
            }

            #region move
            switch (actualDirection)
            {
                case Direction.LEFT:
                    position.X -= Velocity.X;
                    break;
                case Direction.RIGHT:
                    position.X += Velocity.X;
                    break;
                case Direction.UP:
                    position.Y -= Velocity.Y;
                    break;
                case Direction.DOWN:
                    position.Y += Velocity.Y;
                    break;

            }
            #endregion
        }

        public void Die()
        {
            lifes--;
            if (lifes < 0)
            {
                Game1.present_State = Game1.States.INTRO;
                points = 0;
                lifes = Game1.QTD_INICIAL_VIDAS;
            }

            position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
            actualDirection = Direction.RIGHT;

        }
    }
}
